package com.tony.blog.server.service.web.impl;

import com.google.common.collect.Maps;
import com.tony.blog.api.utils.Constant;
import com.tony.blog.model.entity.SysModule;
import com.tony.blog.model.entity.SysUserEntity;
import com.tony.blog.model.entity.UserMsg;
import com.tony.blog.model.mapper.SysUserMapper;
import com.tony.blog.model.mapper.UserMsgMapper;
import com.tony.blog.model.request.IdRequest;
import com.tony.blog.model.request.UserRequest;
import com.tony.blog.server.common.CommonService;
import com.tony.blog.server.service.SysUserService;
import com.tony.blog.server.service.web.IndexUserService;
import org.joda.time.DateTime;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.env.Environment;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.function.Consumer;

/**
 * Created with IntelliJ IDEA.
 *
 * @Title: IndexUserServiceImpl
 * @Auther: 皮蛋布丁
 * @Date: 2021/08/18/21:39
 * @Description:
 */
@Service("indexUserService")
@EnableAsync    //使用多线程,开启异步
public class IndexUserServiceImpl implements IndexUserService {

    @Autowired
    private Environment env;
    @Autowired
    private SysUserService sysUserService;
    @Autowired
    private CommonService commonService;
    @Autowired
    private UserMsgMapper userMsgMapper;
    @Autowired
    private SysUserMapper sysUserMapper;

    /**
    * @Description: registerUser 注册用户
    * @Param: [userRequest]
    * @return: java.lang.Boolean
    * @Author: 皮蛋布丁
    * @Date: 2021/8/18 21:53
    */
    @Override
    public Boolean registerUser(UserRequest userRequest) {
        SysUserEntity entity = new SysUserEntity();
        BeanUtils.copyProperties(userRequest,entity);
        //设置默认用户头像
        entity.setImgUrl(env.getProperty("common.user.img.default"));
        return sysUserService.saveUser(entity);
    }

    /**
    * @Description: getUserMsg 获取用户消息
    * @Param: [userId]
    * @return: java.util.Map<java.lang.String,java.lang.Object>
    * @Author: 皮蛋布丁
    * @Date: 2021/8/19 20:49
    */
    @Override
    public Map<String, Object> getUserMsg(int userId) {
        Map<String,Object>  resMap = Maps.newHashMap();
        if (!Objects.isNull(userId)) {
            List<Constant.MsgModule> list = Arrays.asList(Constant.MsgModule.values());
            list.forEach(msgModule -> {
                //获取当前用户(消息接受者)每个消息模块的所有消息列表(未读的在前面)
                List<UserMsg> msg = userMsgMapper.selectByModuleUIdsRead(msgModule.getCode(),userId,null);
                resMap.put(msgModule.getCode(),msg);
                //取出的消息数据只限N条，同时，统计未读的消息的总数
                resMap.put(msgModule.getCode()+"-unRead",userMsgMapper.countModuleUIdIsRead(msgModule.getCode(),
                        userId,0));
            });
        }
        return resMap;
    }

    /**
    * @Description: uploadImg 上传用户头像
    * @Param: [request, userId]
    * @return: java.lang.String
    * @Author: 皮蛋布丁
    * @Date: 2021/8/21 21:56
    */
    @Override
    public String uploadImg(MultipartHttpServletRequest request, Long userId) throws Exception {
        Map<String,Object> resMap = Maps.newHashMap();
        MultipartFile multipartFile = request.getFile("img");
        Map<String,Object> dataMap = commonService.uploadV2(multipartFile, SysModule.ModuleUser,
                Constant.UpYunRootFolder.head.getValue());
        //更新头像
        sysUserMapper.updateUserImg(userId,String.valueOf(dataMap.get("fileUrl")));
        return env.getProperty("common.file.access.root.url") + dataMap.get("fileUrl");
    }

    /**
    * @Description: readMsg 读消息
    * @Param: [request, userId]
    * @return: java.lang.Boolean
    * @Author: 皮蛋布丁
    * @Date: 2021/9/26 21:36
    */
    @Override
    public Boolean readMsg(IdRequest request, Long userId) {
        UserMsg msg = userMsgMapper.selectByPrimaryKey(request.getId());
        if (!Objects.isNull(msg) && msg.getReceiverId().equals(userId.intValue()) && Objects.equals(0,
                msg.getIsDelete()) && Objects.equals(0,msg.getIsRead())) {
            msg.setIsRead(1);
            msg.setReadTime(DateTime.now().toDate());
            msg.setUpdateTime(DateTime.now().toDate());
            userMsgMapper.updateByPrimaryKey(msg);
            return true;
        }
        return null;
    }
}
